﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Windows.Controls.Ribbon;
using Microsoft.Win32;
using System.Windows.Forms;
using System.IO;
using Super64.Configuration;
using System.Windows.Interop;
using Ookii.Dialogs.Wpf;
using Super64.Core;
using NEmu;
using System.Reflection;
using Super64.Core.Memory.Rom;

namespace Super64
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : RibbonWindow
    {
        private string m_ProgramTitle = "Super64 1.0";

        public MainWindow()
        {
            InitializeComponent();
            m_ProgramTitle = Assembly.GetExecutingAssembly().GetName().Name + " " + Assembly.GetExecutingAssembly().GetName().Version;
            this.Title = m_ProgramTitle;

            #region Event Hooks
            m_MenuItem_Quit.Click += new RoutedEventHandler(m_MenuItem_Quit_Click);
            m_RibbonButton_GameListBrowse.Click += new RoutedEventHandler(m_RibbonButton_GameListBrowse_Click);
            m_RibbonButton_GameListRefresh.Click += new RoutedEventHandler(m_RibbonButton_GameListRefresh_Click);
            m_Button_Stop.Click += new RoutedEventHandler(m_Button_Stop_Click);
            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
            m_Button_Run.Click += new RoutedEventHandler(m_Button_Run_Click);
            #endregion
        }

        #region Event Handlers

        void m_Button_Stop_Click(object sender, RoutedEventArgs e)
        {
            StopMachine();
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Config.LoadConfig();
            BuildGameList();
        }

        void m_RibbonButton_GameListRefresh_Click(object sender, RoutedEventArgs e)
        {
            BuildGameList();
        }

        void m_RibbonButton_GameListBrowse_Click(object sender, RoutedEventArgs e)
        {
            WindowInteropHelper helper = new WindowInteropHelper(this);
            VistaFolderBrowserDialog dlg = new VistaFolderBrowserDialog();
            dlg.UseDescriptionForTitle = true;
            dlg.Description = "Choose a N64 rom directory";
            dlg.ShowDialog();
            GeneralConfig.RomDirectory = dlg.SelectedPath;
            Config.SaveConfig();
            BuildGameList();
        }

        void m_MenuItem_Quit_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Application.Current.Shutdown();
        }

        void m_Button_Run_Click(object sender, RoutedEventArgs e)
        {
            StartMachine();
        }

        #endregion

        void SetWindowMode(bool running)
        {
            this.m_Button_Run.IsEnabled = !running;
            this.m_Button_Pause.IsEnabled = running;
            this.m_Button_Restart.IsEnabled = running;
            this.m_Button_Stop.IsEnabled = running;
            this.m_RibbonButton_GameListBrowse.IsEnabled = !running;
            this.m_RibbonButton_GameListRefresh.IsEnabled = !running;
            this.m_RibbonMenuButton_SortMenu.IsEnabled = !running;
            this.m_DataGrid_GameList.Visibility = running ? Visibility.Hidden : Visibility.Visible;
        }

        void BuildGameList()
        {
            N64Manager.GameList.Clear();

            if (Directory.Exists(GeneralConfig.RomDirectory))
            {
                N64Manager.GameList.GetRomList(GeneralConfig.RomDirectory);
            }

            m_DataGrid_GameList.ItemsSource = N64Manager.GameList;
            m_DataGrid_GameList.Items.Refresh();
        }

        void StartMachine()
        {
            if (m_DataGrid_GameList.SelectedIndex < 0)
            {
                System.Windows.MessageBox.Show("Please select a rom first!", "Error starting machine", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            // Init the machine
            N64Manager.InitMachine();

            // Open the rom
            N64Manager.OpenRom((GameListEntry)m_DataGrid_GameList.Items[m_DataGrid_GameList.SelectedIndex]);

            // Set window state
            SetWindowMode(true);

            // Boot and run VM
            N64Manager.N64VirtualMachine.Run();

            SetWindowTitle(RunState.Running);
        }

        void StopMachine()
        {
            SetWindowTitle(RunState.Stopped);
            this.m_Button_Stop.IsEnabled = false;
            N64Manager.N64VirtualMachine.Stop();
            SetWindowMode(false);
            this.Title = m_ProgramTitle;
        }

        void SetWindowTitle(RunState state)
        {
            this.Title = m_ProgramTitle + " - " + state.ToString();
            CartRom rom = ((N64Resources)N64Manager.N64VirtualMachine.Resources).CurrentRom;

            if (rom != null)
            {
                this.Title += ": " + rom.GameName;
            }
        }
    }
}
